#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>


int fd[2] = {0};
char buf[32] = {0};
char buf1[32] = {0};
pid_t pid;

void RD(){
    while(1){
    read(fd[0], buf, sizeof(buf));
    if(strcmp(buf, "exit") == 0){
        break;
    }
    printf("buf = %s\n", buf);
    }
}
void WR(){
    do{
    gets(buf1);
    write(fd[1], buf1, strlen(buf1) + 1);
    }while(strcmp(buf1, "exit") != 0);
    wait(NULL);
}

int main(){
    if(pipe(fd) < 0){
        perror("pipe err");
        return -1;
    }
    pid = fork();
    if(pid < 0){
        perror("fork err");
        return -1;
    }else if(pid == 0){
        RD();
    }
    else{
        WR();
    }
    return 0;
}